package com.koo.modules.quartz.util;

import com.koo.common.exception.RRException;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class MysqlJDBCUtil {
    
    private static String url;
    private static String userName;
    private static String password;
    
    static Properties properties = new Properties();
    static{
        String sourceFile = "jdbc.properties";
        InputStream in = MysqlJDBCUtil.class.getClassLoader().getResourceAsStream(sourceFile);
        try {
            properties.load(in);
            Class.forName(properties.getProperty("jdbc_driver"));
        } catch (ClassNotFoundException | IOException e) {
            throw new RRException(e);
        }
    }
    
    public static Connection getConnection(String type){
        url = ChangeUtil.decryptValue(properties.getProperty(type+"jdbc_url"));
        userName = ChangeUtil.decryptValue(properties.getProperty(type+"jdbc_username"));
        password = ChangeUtil.decryptValue(properties.getProperty(type+"jdbc_password"));
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, userName, password);
        } catch (SQLException e) {
            throw new RRException(e);
        }
        return connection;
    }
    
    public static Connection setAutoCommitFalse(String type){
        Connection connection = getConnection(type);
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            throw new RRException(e);
        }
        return connection;
    }
    
    public static void rollback(Connection connection){
        try {
            connection.rollback();
        } catch (SQLException e) {
            throw new RRException(e);
        }finally {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RRException(e);
            }
        }
    }
    
    public static void commit(Connection connection){
        try {
            connection.commit();
        } catch (SQLException e) {
            throw new RRException(e);
        }finally {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RRException(e);
            }
        }
    }
    public static void close(Connection connection){
        try {
            connection.close();
        } catch (SQLException e) {
            throw new RRException(e);
        }
    }
    
    public static ResultSet execQuery(String sql,Connection connection, Object...objects){
        try {
            PreparedStatement prepareStatement = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            setParams(prepareStatement, objects);
            ResultSet resultSet = prepareStatement.executeQuery();
            return resultSet;
        } catch (SQLException e) {
            throw new RRException(e);
        }
    }
    
    public static Integer execUpdate(String sql,Connection connection,Object...objects){
        try {
            PreparedStatement prepareStatement = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            setParams(prepareStatement, objects);
            Integer update = prepareStatement.executeUpdate();
            return update;
        } catch (SQLException e) {
            throw new RRException(e);
        }
    }
    
    private static void setParams(PreparedStatement statement, Object...objects) {
        try {
            for (int i = 0; i < objects.length; i++) {
                statement.setObject(i + 1, objects[i]);
            }
        } catch (SQLException e) {
            throw new RRException(e);
        }
    }

}
